#include "MonsterMgr.h"
#include "base/core/log.h"
#include "MonsterRefMgr.h"
#include "Character/CreatureMgr.h"
//#include "Character/newAI/behaviac/CreatureCommonAI.h"
#include "Common/TableData/MonsterMonsterCfg.h"
#include "Common/TableData/BattlenpcBattlenpcCfg.h"
#include "Skill/SkillMgr.h"
MonsterMgr::MonsterMgr()
{
	m_aiEnable = true;
}

MonsterMgr::~MonsterMgr()
{
}

bool MonsterMgr::Init()
{
	m_aiEnable = true;
	//m_monsters.clear();

	const MonsterMonsterCfgMap * pMonsterMap=g_GetMonsterMonsterCfgTable()->GetMonsterMonsterCfgMap();
	if (pMonsterMap)
	{
		MonsterMonsterCfgMap::const_iterator iter = pMonsterMap->begin();
		for (; iter != pMonsterMap->end(); ++iter)
		{
			stMonsterConfig stInfo;
			g_GetSkillMgr()->ParseFormulaParma(iter->second.maxHP, stInfo._maxHP);
			g_GetSkillMgr()->ParseFormulaParma(iter->second.maxMP, stInfo._maxMP);
			g_GetSkillMgr()->ParseFormulaParma(iter->second.ap, stInfo._ap);
			g_GetSkillMgr()->ParseFormulaParma(iter->second.mat, stInfo._mat);
			g_GetSkillMgr()->ParseFormulaParma(iter->second.dp, stInfo._dp);
			g_GetSkillMgr()->ParseFormulaParma(iter->second.mdp, stInfo._mdp);
			g_GetSkillMgr()->ParseFormulaParma(iter->second.phyHit, stInfo._phyHit);
			g_GetSkillMgr()->ParseFormulaParma(iter->second.magHit, stInfo._magHit);
			g_GetSkillMgr()->ParseFormulaParma(iter->second.phyDodge, stInfo._phyDodge);
			g_GetSkillMgr()->ParseFormulaParma(iter->second.magDodge, stInfo._magDodge);
			g_GetSkillMgr()->ParseFormulaParma(iter->second.phyCrit, stInfo._phyCrit);
			g_GetSkillMgr()->ParseFormulaParma(iter->second.magCrit, stInfo._magCrit);
			g_GetSkillMgr()->ParseFormulaParma(iter->second.phyToughness, stInfo._phyToughness);
			g_GetSkillMgr()->ParseFormulaParma(iter->second.magToughness, stInfo._magToughness);
			g_GetSkillMgr()->ParseFormulaParma(iter->second.fireDef, stInfo._fireDef);
			g_GetSkillMgr()->ParseFormulaParma(iter->second.iceDef, stInfo._iceDef);
			g_GetSkillMgr()->ParseFormulaParma(iter->second.windDef, stInfo._windDef);
			g_GetSkillMgr()->ParseFormulaParma(iter->second.solDef, stInfo._solDef);
			g_GetSkillMgr()->ParseFormulaParma(iter->second.rayDef, stInfo._rayDef);
			g_GetSkillMgr()->ParseFormulaParma(iter->second.poisonDef, stInfo._poisonDef);
			g_GetSkillMgr()->ParseFormulaParma(iter->second.phyCritMul, stInfo._phyCritMul);
			g_GetSkillMgr()->ParseFormulaParma(iter->second.magCritMul, stInfo._magCritMul);
			g_GetSkillMgr()->ParseFormulaParma(iter->second.view, stInfo._view);
			g_GetSkillMgr()->ParseFormulaParma(iter->second.normalSkill, stInfo._normalSkill);

			m_mapMonster[iter->first] = stInfo;
		}
	}



	const BattlenpcBattlenpcCfgMap * pBattleNpcMap = g_GetBattlenpcBattlenpcCfgTable()->GetBattlenpcBattlenpcCfgMap();
	if (pBattleNpcMap)
	{
		BattlenpcBattlenpcCfgMap::const_iterator iterNpc = pBattleNpcMap->begin();
		for (; iterNpc != pBattleNpcMap->end(); ++iterNpc)
		{
			stNpcConfig stInfo;
			g_GetSkillMgr()->ParseFormulaParma(iterNpc->second.maxHP, stInfo._maxHP);
			g_GetSkillMgr()->ParseFormulaParma(iterNpc->second.maxMP, stInfo._maxMP);
			g_GetSkillMgr()->ParseFormulaParma(iterNpc->second.ap, stInfo._ap);
			g_GetSkillMgr()->ParseFormulaParma(iterNpc->second.mat, stInfo._mat);
			g_GetSkillMgr()->ParseFormulaParma(iterNpc->second.dp, stInfo._dp);
			g_GetSkillMgr()->ParseFormulaParma(iterNpc->second.mdp, stInfo._mdp);
			g_GetSkillMgr()->ParseFormulaParma(iterNpc->second.phyHit, stInfo._phyHit);
			g_GetSkillMgr()->ParseFormulaParma(iterNpc->second.magHit, stInfo._magHit);
			g_GetSkillMgr()->ParseFormulaParma(iterNpc->second.phyDodge, stInfo._phyDodge);
			g_GetSkillMgr()->ParseFormulaParma(iterNpc->second.magDodge, stInfo._magDodge);
			g_GetSkillMgr()->ParseFormulaParma(iterNpc->second.phyCrit, stInfo._phyCrit);
			g_GetSkillMgr()->ParseFormulaParma(iterNpc->second.magCrit, stInfo._magCrit);
			g_GetSkillMgr()->ParseFormulaParma(iterNpc->second.phyToughness, stInfo._phyToughness);
			g_GetSkillMgr()->ParseFormulaParma(iterNpc->second.magToughness, stInfo._magToughness);
			g_GetSkillMgr()->ParseFormulaParma(iterNpc->second.fireDef, stInfo._fireDef);
			g_GetSkillMgr()->ParseFormulaParma(iterNpc->second.iceDef, stInfo._iceDef);
			g_GetSkillMgr()->ParseFormulaParma(iterNpc->second.windDef, stInfo._windDef);
			g_GetSkillMgr()->ParseFormulaParma(iterNpc->second.solDef, stInfo._solDef);
			g_GetSkillMgr()->ParseFormulaParma(iterNpc->second.rayDef, stInfo._rayDef);
			g_GetSkillMgr()->ParseFormulaParma(iterNpc->second.poisonDef, stInfo._poisonDef);
			g_GetSkillMgr()->ParseFormulaParma(iterNpc->second.phyCritMul, stInfo._phyCritMul);
			g_GetSkillMgr()->ParseFormulaParma(iterNpc->second.magCritMul, stInfo._magCritMul);
			g_GetSkillMgr()->ParseFormulaParma(iterNpc->second.view, stInfo._view);
			g_GetSkillMgr()->ParseFormulaParma(iterNpc->second.normalSkill, stInfo._normalSkill);

			m_mapNpc[iterNpc->first] = stInfo;
		}
	}

	return true;
}


stMonsterConfig * MonsterMgr::GetMonsterConfigParam(uint32_t configID)
{
	mapMonster::iterator iter=m_mapMonster.find(configID);
	if (iter!=m_mapMonster.end())
	{
		return &iter->second;
	}
	return nullptr;
}
stNpcConfig  * MonsterMgr::GetBattleNpcConfigParam(uint32_t configID)
{
	mapNpc::iterator iter = m_mapNpc.find(configID);
	if (iter != m_mapNpc.end())
	{
		return &iter->second;
	}
	return nullptr;
}

bool MonsterMgr::UnInit()
{

	m_mapMonster.clear();
	m_mapNpc.clear();
	/*mapMonster::iterator iter = m_monsters.begin();
	Monster* pMonster = nullptr;
	for (; iter != m_monsters.end(); iter++)
	{
		pMonster = iter->second;
		if (!pMonster)
			continue;
		if (!pMonster->UnInit())
			LogErrFmtPrint("monster unint error can case link memery, monsterCid:%d, MonsterName:%s", pMonster->GetCid(), pMonster->GetCid());

		iter->second = nullptr;
		g_GetCreatureMgr()->RecyleCreature(pMonster);
	}

	m_monsters.clear();*/

	return true;
}
//创建怪物 monsIdx：怪物对应json中所在数组的索引（>= 0） ，默认索引值设置为 -1（有些怪物是动态创建的是没有索引）
//Monster* MonsterMgr::CreateMonster(uint32_t monsterId, uint32_t sceneId, Point3<float> bornPos,int32_t monsIdx /*= -1*/)
//{
//	Monster* pmonster = nullptr;
//	pmonster = dynamic_cast<Monster*>(g_GetCreatureMgr()->MallocCreature(CREATURE_MONSTER));
//
//	if (pmonster == nullptr)
//	{
//		LogErrFmtPrint("monster pool malloc failed");
//		return nullptr;
//	}
//
//	if (!pmonster->Born(g_GetCreatureMgr()->NextId(), monsterId, sceneId, bornPos, monsIdx))
//	{
//		LogErrFmtPrint("create monster fail, monster:");
//		g_GetCreatureMgr()->RecyleCreature(pmonster);
//		return nullptr;
//	}
//
//	//pmonster->SetAiEnable(m_aiEnable);
//	m_monsters[pmonster->GetCid()] = pmonster;
//
//	return pmonster;
//}

//void MonsterMgr::RecyleMonster(Monster* pmonster)
//{
//	if (!pmonster)
//		return;
//	CharIDType cid = pmonster->GetCid();
//	mapMonster::iterator iter = m_monsters.find(cid);
//	if (iter == m_monsters.end())
//		return;
//	m_monsters.erase(iter);
//	if (!pmonster->UnInit())
//		LogErrFmtPrint("monster recyle error can case link memery, monsterCid:%d, MonsterName:%s", pmonster->GetCid(), pmonster->GetCid());
//	
//	g_GetCreatureMgr()->RecyleCreature(pmonster);
//	pmonster = nullptr;
//}

//void MonsterMgr::MonsterDeadHandle(CharIDType monsterCid)
//{
//	monsterReleaseInfo mrelease;
//	mrelease.monsterCid = monsterCid;
//	mrelease.releaseTime = g_GetGlobalServerTime()->Tick();
//	m_releaseMonstlst.push_back(mrelease);
//}

bool MonsterMgr::Update(uint64_t tick)
{
	/*Monster *pmonster = nullptr;
	releaseLst::iterator iter = m_releaseMonstlst.begin();
	for (; iter != m_releaseMonstlst.end();)
	{
		if (iter->releaseTime + 10000 >= tick)
		{
			pmonster = GetMonster(iter->monsterCid);
			if (!pmonster)
				m_releaseMonstlst.erase(iter++);
			else
			{
				RecyleMonster(pmonster);
				iter++;
			}
		}
		else
			iter++;
	}
	return true;*/
	return true;
}

//void MonsterMgr::SetAIEnable(bool use)
//{
//	m_aiEnable = use;
//	for (auto it = m_monsters.begin(); it != m_monsters.end(); it++)
//	{
//		Monster* pMonster = it->second;
//		if (pMonster && pMonster->GetAiData())
//		{
//			pMonster->GetAiData()->SetAiEnable(m_aiEnable);
//		}
//	}
//}

//void MonsterMgr::UpdateAI()
//{
//	/*uint64_t tick = g_GetGlobalServerTime()->Tick();
//	mapMonster::iterator iter = m_monsters.begin();
//	Monster* pMonster = nullptr;
//	for (; iter != m_monsters.end(); iter++)
//	{
//	pMonster = iter->second;
//	if (pMonster)
//	{
//	pMonster->UpdateAiInfo(tick);
//	}
//	}*/
//}
//
//Monster* MonsterMgr::GetMonster(CharIDType monsterCid)
//{
//	mapMonster::iterator iter = m_monsters.find(monsterCid);
//	if (iter == m_monsters.end())
//		return nullptr;
//	return iter->second;
//}
